package 
{
    import com.bit101.components.Label;
    import com.bit101.components.NumericStepper;
    import com.bit101.components.PushButton;
    import com.bit101.components.RadioButton;
    import com.bit101.components.TextArea;
	import flash.display.Sprite;
	import flash.events.Event;
    import flash.display.StageScaleMode;
    import flash.display.StageAlign;
    import flash.geom.Rectangle;
	
	/**
	 * ...
	 * @author ivan
	 */
	public class Chrono extends Sprite 
	{
		
		public function Chrono():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
        
        private var r24:RadioButton;
        private var r25:RadioButton;
        private var r30:RadioButton;
        private var rCustom:RadioButton;
        private var numBox:NumericStepper;
        private var log:TextArea;
        private var resetButton:PushButton;
        private var goButton:PushButton;
        
        private var frameRate:int = 24;
        private var startDate:Date;
        
        private static const startText:String = "Go!";
        
        public function config(sector:String):void
        {
            new Label(this, 30, 30, "Sector: " + sector);
        }
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
            
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            
            r24 = new RadioButton(this, 10, 10, "24 f/s", true, onRadioButton24);
            r25 = new RadioButton(this, 60, 10, "25 f/s", false, onRadioButton25);
            r30 = new RadioButton(this, 110, 10, "30 f/s", false, onRadioButton30);
            rCustom = new RadioButton(this, 10, 35, "Custom", false, onRadioButtonCustom);
            numBox = new NumericStepper(this, 70, 30, numBoxChange);
            numBox.minimum = 1;
            
            goButton = new PushButton(this, 90, 180, startText, onGoButton);
            goButton.width = 60;
            
            resetButton = new PushButton(this, 10, 180, "Reset", onResetButton);
            resetButton.width = 60;
            resetButton.enabled = false;
            
            log = new TextArea(this, 10, 60);
            log.editable = false;
            log.width = 140;
            log.autoHideScrollBar = true;
            
            numBox.visible = false;
            numBox.value = frameRate;
            
		}
		
        private function onRadioButton24(e:Event):void { changeFrameRate(24); }
        private function onRadioButton25(e:Event):void { changeFrameRate(25); }
        private function onRadioButton30(e:Event):void { changeFrameRate(30); }
        
        private function changeFrameRate(fr:int):void
        {
            numBox.visible = false;
            frameRate = fr;
        }
        
        private function onRadioButtonCustom(e:Event):void
        {
            numBox.visible = true;
            frameRate = numBox.value;
        }
        
        private function numBoxChange(e:Event):void
        {
            if (numBox.value <= 0) numBox.value = 1;
            frameRate = numBox.value;
        }
        
        private function onGoButton(e:Event):void
        {
            radioButtonsEnabled(false);
            numBox.enabled = false;
            resetButton.enabled = true;
            goButton.label = "Step";
            
            if (!startDate)
            {
                startDate = new Date();
                log.text = "0\n";
            }
            else 
            {
                var currentDate:Date = new Date();
                var elapsed:Number = currentDate.getTime() - startDate.getTime();
                
                var frames:int = frameRate * elapsed / 1000 + 1;
                
                log.text = log.text + elapsed.toString() + "ms, " + frames.toString() + " frames\n";
            }
            
            // TEST!!!
            (stage.getChildAt(0) as Object).notification("Hello!");
            // (stage.("SectorSelection") as Object).notification("Hello!");
            // TEST!!!
        }
        
        private function onResetButton(e:Event):void
        {
            radioButtonsEnabled(true);
            numBox.enabled = true;
            log.text = "";
            resetButton.enabled = false;
            goButton.label = startText;
            
            startDate = null;
        }
        
        private function radioButtonsEnabled(val:Boolean):void
        {
            r24.enabled = r25.enabled = r30.enabled = rCustom.enabled = val;
        }
	}
	
}